package com.qingfeng.core.utils;

import com.octo.captcha.CaptchaFactory;
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.engine.GenericCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.multitype.GenericManageableCaptchaService;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;

/**
 * @description: 验证码生成工具
 * @author:LWY
 * @date: 2017/10/23 16:14
 * @modified:
 */
public class CaptchaUtil {

    public static final int minAcceptedWordLength = 6;

    public static final int maxAcceptedWordLength = 6;

    public static final int imgWidth = 100;

    public static final int imgHeight = 50;

    public static final int minFontSize = 20;

    public static final int maxFontSize = 25;

    public static final Font font = new Font("@YaHei Consolas Hybrid", Font.BOLD, (minFontSize + maxFontSize) / 2);

    public static final String captchaContent = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    //文字干扰器--- 可以创建多个
    private static RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(new int[]{160, 190}, new int[]{0, 15}, new int[]{0, 15});
    //    private static BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(2,cgen);//气泡干扰
//    private static LineTextDecorator lineTextDecorator = new LineTextDecorator(1,cgen);//曲线干扰
//    private static TextDecorator[] textDecorators = new TextDecorator[]{baffleTextDecorator, lineTextDecorator};
    private static TextPaster testPaster = new DecoratedRandomTextPaster(minAcceptedWordLength, maxAcceptedWordLength, cgen, true, new TextDecorator[]{new BaffleTextDecorator(new Integer(1), Color.WHITE)});

    //private static NonLinearTextPaster testPaster=new NonLinearTextPaster(minAcceptedWordLength, maxAcceptedWordLength, Color.red);

    private static GradientBackgroundGenerator backgroundGen = new GradientBackgroundGenerator(imgWidth, imgHeight, new Color(239, 246, 236), new Color(239, 246, 236));

    private static RandomFontGenerator fontGen = new RandomFontGenerator(minFontSize, maxFontSize, new Font[]{font});

    private static RandomWordGenerator wordGen = new RandomWordGenerator(captchaContent);

    private static ComposedWordToImage wordToImage = new ComposedWordToImage(fontGen, backgroundGen, testPaster);

    private static GimpyFactory captchaFacotry = new GimpyFactory(wordGen, wordToImage);

    private static CaptchaFactory[] factories = new GimpyFactory[]{captchaFacotry};

    private static GenericCaptchaEngine ImageEngine = new GenericCaptchaEngine(factories);

    private static ImageCaptchaService instance = new GenericManageableCaptchaService(ImageEngine, 300, 20000, 20000);

    public static ImageCaptchaService getInstance() {

        return instance;

    }

    public static boolean isResultCorrect(HttpServletRequest request) throws CaptchaServiceException {
        String captchaId = request.getSession().getId();
        String resString = request.getParameter("veriCode");
        boolean isResponseCorrect = getInstance().validateResponseForID(captchaId,
                resString);
        return isResponseCorrect;
    }

}
